package com.mlnx.netty.base.handler;

import com.mlnx.common.utils.MyLog;
import com.mlnx.data.entity.SugarInfo;
import com.mlnx.device.entity.Device;
import com.mlnx.netty.base.message.SugarMessage;
import io.netty.channel.ChannelHandlerContext;

/**
 * Created by amanda.shan on 2018/2/12.
 */
public class PushSugarHandle extends BaseSimpleChannelInboundHandler<SugarMessage> {

    private MyLog log = MyLog.getLog(this.getClass());

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, SugarMessage msg) throws Exception {
        String deviceId = msg.getImei();

        SugarInfo sugarInfo = new SugarInfo();

        sugarInfo.setDeviceId(deviceId);
        sugarInfo.setState(msg.getLabel());
        sugarInfo.setTime(msg.getTestTime());
        sugarInfo.setResult(msg.getResult());
        sugarInfo.setSample(msg.getSample());
        Device device = deviceNettyService.getDevice(deviceId);
        if (device == null) {
            sugarInfo.setPatientId(-1);
            log.info("设备不存在");
        } else {
            if (device.getPatientId() == null || device.getPatientId() == 0) {
                log.info("设备未绑定病人");
            } else {
                sugarInfo.setPatientId(device.getPatientId());
            }

        }
        deviceBroadcastService.castSugarInfo(sugarInfo);


    }
}
